"""
======================
@author:SUNLIN
@time:2025/3/6 19:10
@email:1232@163.com
======================
"""
import requests
from bs4 import BeautifulSoup
from requests.auth import HTTPBasicAuth

import pandas as pd

import time
import random

all_infos = []

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
    'Referer': 'https://www.douban.com/',
    'Accept-Language': 'zh-CN,zh;q=0.9',
}
movie_links = []
movie_names = []

username = '18860660885'
password = 'Zilv.999+'


def get_list(soup_list):
    list = []
    for ele in soup_list:
        list.append(ele.string)
    return list


# 1.访问主页面，并且完成页面跳转
def get_page(page_link):
    current_page = 0
    max_page = 24
    # 获取页面信息
    while current_page <= max_page:
        url = page_link + '?start=' + str(current_page) + "&filter="
        time.sleep(3)  # 随机延时1到3秒
        response = requests.get(url=url, headers=headers, auth=HTTPBasicAuth(username, password))
        get_links(response)
        current_page = current_page + 25
        # exit()


# 2.抓取每个页面的电影链接
def get_links(response):
    soup = BeautifulSoup(response.text, 'html.parser')
    print(soup)
    for ele in soup.find_all(class_="hd"):
        movie_names.append(ele.find(class_="title").string)
        movie_links.append(ele.find('a')['href'])


# 3.根据电影链接，获取基本信息、评分信息
def get_infos(url):
    response = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    # 存储容器，电影信息预览
    movie_info = {}
    # 电影名称
    try:
        movie_info['movie_name'] = soup.find(property="v:itemreviewed").text
        movie_info['director'] = soup.find(rel="v:directedBy").string
        movie_info['writer'] = soup.find_all(class_="attrs")[1].string
        movie_info['actor_list'] = get_list(soup.find_all(rel="v:starring"))
        movie_info['type_list'] = get_list(soup.find_all(property="v:genre"))
        movie_info['language'] = soup.find(string='语言:').next_element  # 获取当前元素的下一个元素
        movie_info['release_date'] = soup.find(property="v:initialReleaseDate").string
        movie_info['run_time'] = soup.find(property="v:runtime").string
        movie_info['average_score'] = soup.find(property="v:average").string
        movie_info['number_of_people'] = soup.find(property="v:votes").string
        movie_info['link'] = url
    except AttributeError:
        print('电影已下架')
    ## 存储所有的电影信息
    all_infos.append(movie_info)


if __name__ == '__main__':
    get_page(page_link='https://movie.douban.com/top250')
    print(movie_links)
    # 浏览所有抓取到的信息
    for name, link in zip(movie_names, movie_links):
        print(name, ":111", link)
        get_infos(link)

    # 将电影信息转为二维表，并存到电子表格中
    data = pd.DataFrame(all_infos)
    data.to_excel("高分电影250部")